var appPath = getAppPath();
var EDIT_IMG_IDX = 0;

$(function(){
	// 检索日期范围
	$("#queryDateStart").datebox({
		formatter:dtFmt,
		parser:dtParser
	});
	$("#queryDateEnd").datebox({
		formatter:dtFmt,
		parser:dtParser
	});
	
	// 初始化列表
	initBuyTmplGrid();
});

function dtFmt(dt){
	var y = dt.getFullYear();
    var m = dt.getMonth()+1;
    var d = dt.getDate();
    return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
}
function dtParser(s){
    if (!s) return new Date();
    var ss = (s.split('-'));
    var y = parseInt(ss[0],10);
    var m = parseInt(ss[1],10);
    var d = parseInt(ss[2],10);
    if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
        return new Date(y,m-1,d);
    } else {
        return new Date();
    }
}

// 订单列表初始化
function initBuyTmplGrid(){
	
	$('#buyTmplGrid').datagrid({
	    url:appPath + "/itbBuyTmpl/getBuyTmplInfoPg.action",
	    height:'full',
	    //remoteSort:false,
	    striped:true,
	    fitColumns:true,
	    queryParams: getQueryParam(),
	    singleSelect:false,
	    // If set to true, clicking a checkbox will always select the row. 
	    // If false, selecting a row will not check the checkbox.
	    selectOnCheck:true,
	    // If true, the checkbox is checked/unchecked when the user clicks on a row. 
	    // If false, the checkbox is only checked/unchecked when the user clicks exactly on the checkbox.
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
  	        {field:'buy_id',title:'购买ID',width:35,align:'center',sortable:false},
  	        {field:'buy_no',title:'购买编号',width:50,align:'center',sortable:false},
  	        {field:'type',title:'模版分类',width:30,align:'center',sortable:false,
  	        	formatter:function(value, rowData, rowIndex){
  	        		var t = '<span style="color:red">未知</span>';
  	        		switch(Number(value||'0')){
  	        			case 1: t="海报"; break;
  	        			case 2: t="多页"; break;
  	        			case 3: t="PPT"; break;
  	        		}
  	        		return t;
	        	}
		    },
  	        {field:'tmpl_id',title:'模版ID',width:35,align:'center',sortable:false},
  	        {field:'user_id',title:'用户ID',width:35,align:'center',sortable:false},
	        {field:'state',title:'状态',width:35,align:'center',sortable:false,
  	        	formatter:function(value, rowData, rowIndex){
  	        		var s = '<span style="color:blue">未知</span>';
  	        		switch(Number(value||'0')){
  	        			case 11: s='<span style="color:green">已支付</span>'; break;
  	        			case 12: s='<span style="color:black">支付失败</span>'; break;
  	        			case 60: s='<span style="color:red">过期失效</span>'; break;
  	        		}
  	        		return s;
	        	}
		    },
		    {field:'valid_time_start',title:'有效期(起始)',width:60,align:'center',sortable:false},
		    {field:'valid_time_end',title:'有效期(截止)',width:60,align:'center',sortable:false},
  	        {field:'pay_platform',title:'支付平台',width:30,align:'center',sortable:false,
  	        	formatter:function(value, rowData, rowIndex){
  	        		var pf = '<span style="color:red">未知</span>';
  	        		switch(Number(value||'0')){
  	        			case 10: pf="微信"; break;
  	        			case 20: pf="支付宝"; break;
  	        		}
  	        		return pf;
	        	}
		    },
	        {field:'total_fee',title:'支付金额(分)',width:50,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  return value||0;
		    	  }
		      },
	        {field:'tmpl_price',title:'模版价格(分)',width:50,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  return value||0;
		    	  }
		      },
	        {field:'coupon_fee',title:'优惠金额(分)',width:50,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  return value||0;
		    	  }
		      },
	        {field:'create_time',title:'创建时间',width:40,align:'center',sortable:false},
	        {field:'update_time',title:'更新时间',width:40,align:'center',sortable:false}
	    ]],
	    hideColumn:['user_type'],
	    //view: imgView,// 自定义View
	    pagination:true,
		rownumbers:true,
		toolbar:[
			/*{
				text:'删除订单',
				iconCls:'icon-cancel',
				handler:function(){
					delOrder();
				}
			}*/
		]
	});
}

/**
 * 获取请求信息(订单列表)
 */
function getQueryParam(){
	var queryObject = {};
	
	var findType = $("#queryType").combobox('getValue');
	var findState = $("#queryState").combobox('getValue');
	var findPayPf= $("#queryPayPf").combobox('getValue');
	var findTmplId = $("#queryTmplId").val();
	var findUserId = $("#queryUserId").val();
	
	var findDtS = $("#queryDateStart").datebox('getValue');
	var findDtE = $("#queryDateEnd").datebox('getValue');
	
	if(findType && Number(findType||'0')>0) queryObject.findType = findType;// 分类
	if(findTmplId && Number(findTmplId||'0')>0) queryObject.findTmplId = findTmplId;
	if(findUserId && Number(findUserId||'0')>0) queryObject.findUserId = findUserId;
	if(findState && Number(findState||'0')>0) queryObject.findState = findState;// 状态
	if(findPayPf && Number(findPayPf||'0')>0) queryObject.findPayPf = findPayPf;// 支付平台

	if(findDtS && findDtS.length>0) queryObject.findDateStart = findDtS+' 00:00:00';
	if(findDtE && findDtE.length>0) queryObject.findDateEnd = findDtE+' 23:59:59';
	
	return queryObject;
}


/**
 * 查询数据
 */
function doQuery(){
	var findDtS = $("#queryDateStart").datebox('getValue');
	var findDtE = $("#queryDateEnd").datebox('getValue');
	if(findDtS && findDtS.length>0){
		var chkRet = chkInputDate(findDtS);
		$("#queryDateStart").datebox('setValue',chkRet);
	}
	if(findDtE && findDtE.length>0){
		var chkRet = chkInputDate(findDtE);
		$("#queryDateEnd").datebox('setValue',chkRet);
	}
	
	$("#buyTmplGrid").datagrid('unselectAll');
	$("#buyTmplGrid").datagrid('load', getQueryParam());
}

// 判断日期格式[YYYY-MM-DD]
function chkInputDate(dt){
	var strDate=dt+'';
	
//	var re =/^(\d{1,4})-(\d{1,2})-(\d{1,2})$/;
//	if(re.test(strDate)) {
//		var chkDt=new Date(RegExp.$1, parseInt(RegExp.$2,10)-1, RegExp.$3);
//		if(((chkDt.getFullYear()==parseInt(RegExp.$1))
//			&&((chkDt.getMonth()+1)==parseInt(RegExp.$2,10))
//			&&(chkDt.getDate()==parseInt(RegExp.$3)))) {
//			return strDate;
//		}
//	}
	
    var ss = (strDate.split('-'));
    var y = parseInt(ss[0],10);
    var m = parseInt(ss[1],10);
    var d = parseInt(ss[2],10);
    if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
        var chkDt = new Date(y,m-1,d);
		if((chkDt.getFullYear()==y) &&((chkDt.getMonth()+1)==m) &&(chkDt.getDate()==d)) {
        	return dtFmt(chkDt);
        }
    }
    return '';
}

function fmtCreateTime(value, rowData, rowIndex){
	if(!value)return "";
	var s = new Date(value);
	return rowData.create_time=$.DateUtil.format(s,'yyyy/MM/dd hh:mm:ss');
}
